/*
**	Anderson Ferminiano
**	contato@andersonferminiano.com -- feel free to contact me for bugs or new implementations.
**	jQuery ScrollPagination
**	28th/March/2011
**	http://andersonferminiano.com/jqueryscrollpagination/
**	You may use this script for free, but keep my credits.
**	Thank you.
*/

(function( $ ){
    $.fn.scrollPagination = function(options) {
        var opts = $.extend($.fn.scrollPagination.defaults, options);  
        var target = opts.scrollTarget;
        if (target == null){
            target = obj; 
        }
        opts.scrollTarget = target;
        return this.each(function() {
        //  if(opts.isstart == 'start'){
                $.fn.scrollPagination.init($(this), opts);
        //  }

        });
    };
    $.fn.stopScrollPagination = function(){
        return this.each(function() {
            $(this).attr('scrollPagination', 'disabled');
        });
    };

    $.fn.scrollPagination.loadContent = function(obj, opts){
        var target = opts.scrollTarget;

        var mayLoadContent = $(target).scrollTop()+opts.heightOffset >= opts.heightId.height() - $(target).height();
        //根据mayLoadContent 和 lock两个参数进行判断  
        if (mayLoadContent && opts.lock){
            if (opts.beforeLoad != null){
                   opts.beforeLoad(); 
            }

            //加载数据的时候把lock设为false
            opts.lock = false; 
            $(obj).children().attr('rel', 'loaded');
            $.ajax({
                type: 'POST',
                url: opts.contentPage,
                data: opts.contentData,
                success: function(resp){
                    //加载成功后把lock设为true，可以进行下一次request  
                    opts.lock = true;
                    resp = $.parseJSON(resp);
                    if(resp.reqStatus){
                        var list = resp.data.list;
                        var list_html = '',over_html = '';
                        $.each(list,function(i,item){

                            if(Number(item.is_end)==1) {
                                item.surplus = 0;
                            }

                            list_html += '<li>' +
                                '              <div class="list-head f-cb">' +
                                '                  <img src="'+item.logo+'" alt="'+item.title+'" class="logo fl">' +
                                '                      <div class="label-box fr">' +
                                item.tag_span +
                                '                      </div>' +
                                '               </div>' +
                                '               <div class="list-content">';
                            if(item.cate_id==1){
                                list_html+='        <div class="invest">' +
                                    '                   <p class="money"><span>'+item.investment_money+'</span>元</p>' +
                                    '                   <p>投资金额</p>' +
                                    '               </div>';
                            }
                            if(item.cate_id==4){
                                over_html = '            <div class="invest">' +
                                    '                       <p class="num"><span>'+item.sub_num+'</span></p>' +
                                    '                       <p>需求总数</p>' +
                                    '                   </div>';
                            }else{
                                over_html = '            <div class="invest">' +
                                    '                       <p class="num"><span>'+item.surplus+'/'+item.sub_num+'</span>份</p>' +
                                    '                       <p>剩余份数</p>' +
                                    '                   </div>';
                            }

                            if(item.cate_id==3){
                                list_html+='        <div class="invest">' +
                                    '                   <p class="money"><span>'+item.max_investment_money+'</span>元</p>' +
                                    '                   <p>投资金额</p>' +
                                    '               </div>';

                                over_html = '            <div class="invest">' +
                                    '                       <p class="num"><span>'+item.surplus_money+'/'+item.investment_money_amount+'</span>元</p>' +
                                    '                       <p>剩余额度</p>' +
                                    '                   </div>';
                            }

                            list_html+=over_html;
                            if(item.cate_id==1) {
                                list_html += '      <div class="invest">' +
                                    '                   <p class="commission"><span>'+item.profit_amount+'</span>元</p>' +
                                    '                   <p class="zs_toggle">预期总收益<font class="iconfont">&#xe633;</font></p>' +
                                    '               </div>' +
                                    '           </div>'+
                                '               <div class="earn-detail">' +
                                '                   <p>收益详情：</p>' +
                                '                   <div class="part-parent">' +
                                '                       <div class="child">' +
                                '                           <div class="part">'+item.commission+'</div>' +
                                '                           <div class="part">+</div>' +
                                '                           <div class="part">'+item.expected_money+'</div>' +
                                '                           <div class="part">+</div>' +
                                '                           <div class="part">'+item.bonus+'</div>' +
                                '                       </div>' +
                                '                       <div class="child">' +
                                '                           <div class="part">佣金</div>' +
                                '                           <div class="part">&nbsp;</div>' +
                                '                           <div class="part">理财收益</div>' +
                                '                           <div class="part">&nbsp;</div>' +
                                '                           <div class="part">红包</div>' +
                                '                       </div>' +
                                '                   </div>' +
                                '               </div>';
                            }
                            if(item.cate_id==3) {
                                list_html += '      <div class="invest">' +
                                    '                   <p class="commission"><span>'+item.commission_per+'</span>%</p>' +
                                    '                   <p class="zs_toggle">佣金比例<!--<font class="iconfont">&#xe633;</font>--></p>' +
                                    '               </div>' +
                                    '           </div>';
                                    // '               <div class="earn-detail">' +
                                    // '                   <p>收益详情：</p>' +
                                    // '                   <div class="part-parent">' +
                                    // '                       <div class="child">' +
                                    // '                           <div class="part">'+item.profit_amount+'</div>' +
                                    // '                           <div class="part">+</div>' +
                                    // '                           <div class="part">'+item.interest_rate+'%</div>' +
                                    // '                           <div class="part">+</div>' +
                                    // '                           <div class="part">'+item.bonus+'</div>' +
                                    // '                       </div>' +
                                    // '                       <div class="child">' +
                                    // '                           <div class="part">佣金</div>' +
                                    // '                           <div class="part">&nbsp;</div>' +
                                    // '                           <div class="part">理财收益（年化）</div>' +
                                    // '                           <div class="part">&nbsp;</div>' +
                                    // '                           <div class="part">红包</div>' +
                                    // '                       </div>' +
                                    // '                   </div>' +
                                    // '               </div>';
                            }
                            if(item.cate_id==2 || item.cate_id==4) {
                                list_html += '      <div class="invest">' +
                                    '                   <p class="commission"><span>'+item.commission+'</span>元</p>' +
                                    '                   <p class="zs_toggle">佣金</p>' +
                                    '               </div>' +
                                    '           </div>';
                            }

                            list_html += '  <div class="list-btn-detail">';
                            if(Number(item.is_end)==1) {
                                list_html += '  <a href="javascript:;" class="btn-detail" style="background: #d4d4d4">已结束</a>';
                            }else if(Number(item.can_recieve) == 0) {
                                list_html += '  <a href="javascript:;" class="btn-detail" style="background: #d4d4d4">已领完</a>';
                            }else{
                                list_html += '  <a href="' + item.link + '" class="btn-detail">查看详情</a>';
                            }
                            list_html += '  </div></li>';
                        });
                        $(obj).append(list_html);
                    }

                    var objectsRendered = $(obj).children('[rel!=loaded]');

                    if (opts.afterLoad != null){
                            opts.afterLoad(objectsRendered);	
                    }
                },
                dataType: 'html'
            });
        }
    };
    $.fn.scrollPagination.init = function(obj, opts){
        var target = opts.scrollTarget;
        $(obj).attr('scrollPagination', 'enabled');
        if(opts.initLoad != null){
            opts.initLoad();
        }
        $(target).scroll(function(event){
            if ($(obj).attr('scrollPagination') == 'enabled'){
                $.fn.scrollPagination.loadContent(obj, opts);		
            }
            else {
                event.stopPropagation();	
            }
        });
        $.fn.scrollPagination.loadContent(obj, opts);
    };

    $.fn.scrollPagination.defaults = {
        'contentPage' : null,
        'contentData' : {},
        'beforeLoad': null,
        'afterLoad': null	,
        'scrollTarget': null,
        'heightOffset': 0,
        //添加lock机制，如果数据加载完了，则lock为true，可以加载下一组数据，如果数据没有加载完，则lock为false，等到数据加载完成了为true。  
        'lock':true,
        'initLoad':null//插件初始化时调用的方法
    };	
})( jQuery );